import { IRouter } from '@/global/type/menu'
import type { RouteRecordRaw, RouteComponent } from 'vue-router'

const allComponent = import.meta.glob(`@/views/**/*.vue`, {
  eager: true,
  import: 'default'
})
function initRouter(menu: IRouter[]) {
  const routerMenu: RouteRecordRaw[] = []
  if (menu.length) {
    menu.forEach((item) => {
      const currentComPath = `/src/views/main${item.component}.vue`
      const newRouter: RouteRecordRaw = {
        path: item.path,
        name: item.name,
        component: allComponent[currentComPath] as RouteComponent
      }
      if (item.redirect) {
        newRouter.redirect = item.redirect
      }
      if (item.children && item.children.length !== 0) {
        newRouter.children = initRouter(item.children)
        // routerMenu.push(newRouter)
      }
      // router.addRoute('main', newRouter)
      routerMenu.push(newRouter)
    })
    return routerMenu
  } else {
    return
  }
}

export default initRouter
